package com.tomorrow.runner.data.model;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

import com.tomorrow.runner.data.AbsData;
import com.tomorrow.runner.data.DatabaseHelper;
import com.tomorrow.runner.data.RunProvider;

import java.util.HashSet;

import cn.sucun.android.utils.SQLUtility;

/**
 *
 * Created by tomorrow on 16-4-27.
 */
public class ActivityModel extends AbsData {

    public static final String DATABASE = "activity.db";
    public static final int DATABASE_VERSION = 2;

    public static final String TABLE_NAME = "activity_info";
    public static final String CONTENT_NAME = "activity";

    public static final String ACTIVITY_TYPE = "activity_type";

    public static final String TITLE = "title";

    public static final String DESC_CONTENT = "desc_content";

    public static final String OWNER = "owner";

    public static final String POSITION = "position";

    public static final String WEATHER = "weather";

    public static final String START_TIME = "start_time";

    public static final String END_TIME = "end_time";

    static final String CONTEXT_ID = "context_id";

    public static final int TYPE_RUN = 0;

    public static final int TYPE_PUSH_UP = 1;

    public static final int TYPE_DRINK = 2;

    protected static final HashSet<String> COLUMNS_INT;
    protected static final HashSet<String> COLUMNS_LONG;
    protected static final HashSet<String> COLUMNS_STR;
    protected static final HashSet<String> COLUMNS_BOOL;
    protected static final HashSet<String> COLUMNS_DOUBLE;

    static {
        COLUMNS_INT = new HashSet<>();
        COLUMNS_LONG = new HashSet<>();
        COLUMNS_STR = new HashSet<>();
        COLUMNS_BOOL = new HashSet<>();
        COLUMNS_DOUBLE = new HashSet<>();

        COLUMNS_INT.add(_ID);
        COLUMNS_INT.add(ACTIVITY_TYPE);
        COLUMNS_INT.add(CONTEXT_ID);

        COLUMNS_STR.add(WEATHER);
        COLUMNS_STR.add(TITLE);
        COLUMNS_STR.add(DESC_CONTENT);
        COLUMNS_STR.add(OWNER);
        COLUMNS_STR.add(POSITION);

        COLUMNS_LONG.add(START_TIME);
        COLUMNS_LONG.add(END_TIME);

    }

    public ActivityModel(Cursor c) {
        super(c, true, COLUMNS_STR, COLUMNS_INT, COLUMNS_LONG, COLUMNS_BOOL,COLUMNS_DOUBLE);
    }

    private static Uri sContentUri = null;
    /**
     * 获取数据库操作的uri
     * @return 数据库操作的uri
     */
    public static Uri getContentUri() {
        if (sContentUri == null) {
            sContentUri = Uri.withAppendedPath(RunProvider.getContentUri(),CONTENT_NAME);
        }
        return sContentUri;
    }
    @Override
    protected Uri getBaseUri() {
        return getContentUri();
    }

    @Override
    public void setString(String key, String value) {
        super.setString(key, value);
    }

    @Override
    public void commitChange(ContentResolver cr) {
        super.commitChange(cr);
    }

    /**
     * 通过帮助类继承SQLiteOpenHelper的帮助类对象 创建数据库
     */
    public static final DatabaseHelper.DBBuilder BUILDER = new DatabaseHelper.AbsDBBuilder(TABLE_NAME) {

        @Override
        public void createTable(Context context, SQLiteDatabase db) {
            StringBuilder sb = new StringBuilder();
            sb.append(_ID).append(" INTEGER PRIMARY KEY AUTOINCREMENT, ");
            sb.append(ACTIVITY_TYPE).append(" INTEGER NOT NULL , ");

            sb.append(TITLE).append(" TEXT, ");
            sb.append(DESC_CONTENT).append(" TEXT, ");
            sb.append(OWNER).append(" TEXT NOT NULL, ");
            sb.append(POSITION).append(" TEXT , ");

            sb.append(WEATHER).append(" TEXT , ");

            sb.append(START_TIME).append(" LONG NOT NULL , ");
            sb.append(END_TIME).append(" LONG NOT NULL , ");

            sb.append(CONTEXT_ID).append(" LONG");

            SQLUtility.createTable(db, TABLE_NAME, sb.toString());
        }

        @Override
        public boolean updateTable(SQLiteDatabase db, int oldVersion, int newVersion) {
            return newVersion > oldVersion;
        }
    };
}
